# 作用域
# 全局变量
a = 100


def test1():
    print("这是test1 a的值>>", a)


def test2():
    # 局部变量
    a = 120
    print("这是test2 a的值>>", a)


test1()
test2()
# 全局变量a 的值没有被覆盖, test2() 中的a为局部变量
print("调用函数后a的值>>", a)


# 函数中修改全局变量值
# global
def test3():
    # 申明全局变量
    global a
    a = 120
    print("这是test3 a的值>>", a)


test3()
print("调用test3后a的值>>", a)


def study():
    global name, age
    name = "python基础"
    age = 18
    print(f"{age}我们在学习{name}")


study()


def work():
    print(name)


work()

# nonlocal 申明上层局部变量

a = 10


def outer():
    a = 5

    def inner():
        # nonlocal a
        a = 20

        def inner2():
            nonlocal a
            a = 30
            print("inner2函数中a的值>>", a)

        inner2()
        print("inner函数中a的值>>", a)

    inner()
    print("outer函数中a的值>>", a)


outer()

# 匿名函数 lambda结果为返回值
add = lambda m, n: m + n
print(add(1, 2))
func = lambda name, age=18: (name, age)
print(func("zangsan"))

fune = lambda a, b, c=12: a + b + c
print(fune(1, 2, 3))
print(fune(1, 2))
# 关键字参数
fund = lambda **kwargs: kwargs
print(fund(name="zhansan", age=19))
# lambda 结合if判断
a = 5
b = 8
print("a比b小") if a < b else print("a大于等于b")
comp = lambda a, b: "a比b小" if a < b else "a大于等于b"
print(comp(7, 5))
# 内置函数
import builtins

# 所有内置函数
print(dir(builtins))
# abs() 返回绝对值
print(abs(-10))
# sum 求和
print(sum([1, 2, 3]))
print(sum({4, 5, 6}))
# min max 最大最小值
print(min(1, 2, 3))
print(max(23, 5, 3, 534))
print(min(-32, 6, key=abs))
# zip 将可迭代对象作为参数 ，将对象中元素打包成元组

li = [1, 2, 3]
li2 = ['a', 'b', 'c']
print(zip(li, li2))
# for 循环
for i in zip(li, li2):
    print(i)
    print(type(i))
# list 转换
print(list(zip(li, li2)))

# map()
li = [1, 2, 3]


def funa(x):
    return x * 5


mp = map(funa, li)
print(mp)
for i in mp:
    print(i)
print(list(mp))

# reduce() # 只能两个参数的函数
from functools import reduce

li = [1, 2, 3, 4]


def add(x, y):
    return x + y


res = reduce(add, li)
print(res)

# 拆包
# 方式1
tua = (1, 2, 3, 4)
a, b, c, d = tua
print(a, b, c, d)
# 方式2
a, *b = tua
print(a, b)
print(type(b))

